package com.code;

import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public abstract class JsonUtil {
    private static final DateJsonValueProcessor dateJsonValueProcessor=new DateJsonValueProcessor();

    private static JsonConfig encodeConfig;
    public static JsonConfig encodeConfig(){
    	if (encodeConfig==null){
    		encodeConfig = new JsonConfig();   
    		encodeConfig.setIgnoreDefaultExcludes(false);   
    		encodeConfig.registerJsonValueProcessor(Date.class, dateJsonValueProcessor);
    		encodeConfig.registerJsonValueProcessor(java.sql.Date.class, dateJsonValueProcessor);
    		encodeConfig.registerJsonValueProcessor("rtime", dateJsonValueProcessor);
    		encodeConfig.registerJsonValueProcessor("Date_Time", dateJsonValueProcessor);
    		encodeConfig.registerJsonValueProcessor("InputDate", dateJsonValueProcessor);
    	}
    	return encodeConfig;
    }
    private static JsonConfig decodeConfig;
    public static JsonConfig decodeConfig(){
    	if (decodeConfig==null){
    		decodeConfig = new JsonConfig();   
    		decodeConfig.setIgnoreDefaultExcludes(false);   
    		decodeConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
    		decodeConfig.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessor());
    		decodeConfig.registerJsonValueProcessor("rtime", new JsonDateValueProcessor());
    		decodeConfig.registerJsonValueProcessor("Date_Time", new JsonDateValueProcessor());
    		decodeConfig.registerJsonValueProcessor("InputDate", new JsonDateValueProcessor());
    		decodeConfig.registerJsonValueProcessor("条件表达式", new JsonStringValueProcessor());
    	}
    	return decodeConfig;
    }
    public static String fromObject(Object object){
    	return JSONObject.fromObject(object,encodeConfig()).toString();
    }
    public static String fromArray(Object object){
    	return JSONArray.fromObject(object,encodeConfig()).toString();
    }
    public static JSONObject fromObject(String object){
    	return JSONObject.fromObject(object);
    }
    public static Date getDate(JSONObject json,String key){
    	String value=json.getString(key);
    	if (value==null||"null".equalsIgnoreCase(value)) return null;
    	return dateJsonValueProcessor.parse(value);
    }
    public static Integer getInteger(JSONObject json,String key){
    	String value=json.getString(key);
    	if (value==null||"null".equalsIgnoreCase(value)) return null;
    	return Integer.valueOf(value);
    }
    public static Object getLong(JSONObject json, String key) {
    	String value=json.getString(key);
    	if (value==null||"null".equalsIgnoreCase(value)) return null;
    	return Long.valueOf(value);
	}
    public static Double getDouble(JSONObject json,String key){
    	String value=json.getString(key);
    	if (value==null||"null".equalsIgnoreCase(value)) return null;
    	return Double.valueOf(value);
    }
    public static Boolean getBoolean(JSONObject json,String key){
    	String value=json.getString(key);
    	if (value==null||"null".equalsIgnoreCase(value)) return null;
    	return Boolean.valueOf(value);
    }
    public static String getString(JSONObject json,String key){
    	return json.getString(key);
    }
    @SuppressWarnings("unchecked") 
	public static <T> List<T> toList(String json,Class<T> requiredType){
    	JSONArray ja=JSONArray.fromObject(json);
    	List<T> sl=ja.subList(0, ja.size());
    	return sl;
    }
    @SuppressWarnings("unchecked")
	public static List<Map<String,Object>> toMapList(String json){
    	JSONArray ja=JSONArray.fromObject(json);
    	List<Map<String,Object>> sl=ja.subList(0, ja.size());
    	return sl;
    }
    public static Object[] toArray(String json){
    	Object[] as=JSONArray.fromObject(json).toArray();
    	for (int i=0;i<as.length;i++){
    		if (JSONArray.class.isInstance(as[i])){
    			Object[] a=((JSONArray)as[i]).toArray();
    			as[i]=a;
    			for (int j=0;j<a.length;j++){
    				if (JSONNull.class.isInstance(a[j])){
    					a[j]=null;
    				}
    			}
    		}
    	}
    	return as;
    }
    public static boolean isEmpty(String data){
    	return data==null||data.trim().length()==0;
    }
    public static boolean notEmpty(String data){
    	return data!=null&&data.trim().length()>0;
    }
	
}
